package com.ycsj.goldmedalnewconcept.teacher.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ycsj.common.adapter.VpCommonAdapter;
import com.ycsj.common.base.BaseLazyFragment;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.view.vp.SuperViewPager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.TkKeys;
import com.ycsj.goldmedalnewconcept.common.bean.SubListBean;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.teacher.bean.AnswerInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.ReadLvInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 阅读理解填空
 * Created by Administrator on 2018/12/11 0011.
 */

public class ReadGapFillingFragment extends BaseLazyFragment implements ViewPager.OnPageChangeListener {


    //学生做题0  1学生查看解析 2老师查看题和解析-有人做(全校正确率和小题正确率都显示) 3(习题分析)老师查看题和解析-没人做,对应查看单题
    private String type;

    //阅读理解判断题的所有小题数据
    private List<SubListBean> subListBeans;

    //页面数据对象
    private TkItemBean pageInfo;

    //题目类型
    private TextView tvType;
    //标题
    private TextView tvTitle;
    //题目内容
    private TextView tvContent;
    private SuperViewPager vp;
    //vp的页面指示器
    private TextView tvNum;

    //填充ViewPager的Framgent集合
    private List<Fragment> mFragments;

    //vp的适配器
    private VpCommonAdapter adapter;

    //所有小题的解析的数组
    private String[] analysis;

    //题目类型
    private String tvTitleType;

    //当前大题在整个题目中所处的位置
    private int currPos;

    public static final ReadGapFillingFragment newInstance(TkItemBean pageInfo, String type, int currPos) {
        ReadGapFillingFragment readGapFillingFragment = new ReadGapFillingFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", pageInfo);
        bundle.putString("type", type);
        bundle.putInt("currpos", currPos);
        readGapFillingFragment.setArguments(bundle);
        return readGapFillingFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_read_gap_filling;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //获取页面对象
        initIntent();

        //初始化视图
        initView(view);

        //初始化页面数据
        initData();
    }

    /**
     * 获取页面对象
     */
    private void initIntent() {


        //获取传递的对象
        pageInfo = (TkItemBean) getArguments().getSerializable("data");

        type = getArguments().getString("type");

        //获取当前大题在整个题目中所处的位置
        currPos = getArguments().getInt("currpos");



        //获取所有小题的解析
        String allAnalysis = pageInfo.getAnalysis();
        analysis = allAnalysis.split("￠");

        //获取阅读判断的所有小题的数据集合
        subListBeans = pageInfo.getSub_list();
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvType = view.findViewById(R.id.tv_type);
        tvTitle = view.findViewById(R.id.tv_title);
        tvContent = view.findViewById(R.id.tv_content);
        vp = view.findViewById(R.id.vp);
        //Vp设置监听
        vp.addOnPageChangeListener(this);
        tvNum = view.findViewById(R.id.tv_num);

        //获取标题的内容
        if (TextUtils.isEmpty(pageInfo.getN_topic_name())) {
            tvTitleType = pageInfo.getN_type_name();
        } else {
            tvTitleType = pageInfo.getN_type_name() + "-" + pageInfo.getN_topic_name();
        }
    }


    /**
     * 初始化页面的数据,包含阅读的内容,以及提供选择的
     */
    private void initData() {

        if (subListBeans != null) {

            //设置题目类型
            tvType.setText(tvTitleType);

            //获取题干内容
            String questionContent = pageInfo.getContent();
            tvContent.setText(questionContent);

            //创建底部选择答案的Fragment页面的数据集合
            List<AnswerInfo> answerInfos = new ArrayList<>();

            for (int i = 0; i < subListBeans.size(); i++) {

                AnswerInfo info = new AnswerInfo();

                //设置题目内容
                info.qustionContent = (i + 1) + "." + subListBeans.get(i).getTitle();

                //设置正确答案,阅读理解填空有多个空的情况,服务器返回的正确答案格式为:
                //如果是一个空,即为正确答案,如果是多空,每个空的答案以;连接
                //针对每个空,可能有多个正确答案,正确答案之间以/连接
                info.rightAnswer = subListBeans.get(i).getOption();

                //配置小题的解析
                try {
                    if (TextUtils.isEmpty(pageInfo.getAnalysis())) {
                        info.explain = "略";
                    } else {
                        info.explain = analysis[i];
                    }
                } catch (Exception e) {
                    info.explain = "略";
                }


                //选择答案的集合
                List<ReadLvInfo> readInfos = new ArrayList<>();

                info.readLvInfos = readInfos;

                //配置用户选择的答案
                info.chooseAnswer = subListBeans.get(i).getStu_option();

                //配置全校正确率
                info.proba = subListBeans.get(i).getYes_pro();

                answerInfos.add(info);
            }

            //根据获取的answerInfos来创建填充ViewPager的Fragment
            mFragments = new ArrayList<>();
            if (answerInfos.size() != 0) {
                for (int i = 0; i < answerInfos.size(); i++) {
                    mFragments.add(ItemReadFillingFragment.newInstance(answerInfos.get(i), i, Integer.parseInt(type)));
                }

                //设置底部的VP的指示器数字
                tvNum.setText("1/" + mFragments.size());

                //设置ViewPager的数据
                adapter = new VpCommonAdapter(getChildFragmentManager(), mFragments);
                vp.setAdapter(adapter);
                vp.setOffscreenPageLimit(mFragments.size());
            }
        }
    }


    /**
     * ViewPager的滚动监听事件
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvNum.setText((position + 1) + "/" + mFragments.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 事件处理
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        //点击答题面板将VP位置切换成小题的位置
        if (null != busInfo && currPos == busInfo.bigPos&&busInfo.type == TkKeys.TKCOMIT) {
            //切换对应的小题位置
            vp.setCurrentItem(busInfo.postion);
        }
    }
}
